package com.leyou.upload.service;

import com.aliyun.oss.OSS;
import com.aliyun.oss.common.utils.BinaryUtil;
import com.aliyun.oss.model.MatchMode;
import com.aliyun.oss.model.PolicyConditions;
import com.leyou.common.constant.LyConstants;
import com.leyou.common.exception.domain.ExceptionEnum;
import com.leyou.common.exception.domain.LyException;
import com.leyou.upload.config.OSSProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.*;

@Service
public class UploadService {

    @Autowired
    private OSS client;

    @Autowired
    private OSSProperties prop;

    private static final List ALLOW_IMAGE_TYPE = Arrays.asList("image/jpeg");

    public String localUploadImage(MultipartFile file){

        //获取图片的mime类型
        String imageMimeType = file.getContentType();
        if(!ALLOW_IMAGE_TYPE.contains(imageMimeType)){
            throw new LyException(ExceptionEnum.INVALID_FILE_TYPE);
        }
        //解析图片内容
        BufferedImage bufferedImage = null;
        try {
            bufferedImage = ImageIO.read(file.getInputStream());
        }catch (Exception e){
            throw new LyException(ExceptionEnum.INVALID_FILE_TYPE);
        }
        if(bufferedImage==null){
            throw new LyException(ExceptionEnum.INVALID_FILE_TYPE);
        }

        //得到上传文件的图片名称
        String imageName = UUID.randomUUID() + file.getOriginalFilename();
        //得到上传文件夹所在文件对象
        File imagePathFile = new File(LyConstants.IMG_PATH);
        //上传图片
        try {
            file.transferTo(new File(imagePathFile, imageName));
        }catch (Exception e){
            throw new LyException(ExceptionEnum.FILE_UPLOAD_ERROR);
        }
        return LyConstants.IMG_URL+imageName;

    }

    public Map<String, Object> getAliOssSignature() {
        try {
            long expireTime = prop.getExpireTime();
            long expireEndTime = System.currentTimeMillis() + expireTime * 1000;
            Date expiration = new Date(expireEndTime);
            PolicyConditions policyConds = new PolicyConditions();
            policyConds.addConditionItem(PolicyConditions.COND_CONTENT_LENGTH_RANGE, 0, 1048576000);
            policyConds.addConditionItem(MatchMode.StartWith, PolicyConditions.COND_KEY, prop.getDir());

            String postPolicy = client.generatePostPolicy(expiration, policyConds);
            byte[] binaryData = postPolicy.getBytes("utf-8");
            String encodedPolicy = BinaryUtil.toBase64String(binaryData);
            String postSignature = client.calculatePostSignature(postPolicy);

            Map<String, Object> respMap = new LinkedHashMap<String, Object>();
            respMap.put("accessId", prop.getAccessKeyId());
            respMap.put("policy", encodedPolicy);
            respMap.put("signature", postSignature);
            respMap.put("dir", prop.getDir());
            respMap.put("host", prop.getHost());
            respMap.put("expire", expireEndTime);
            return respMap;
        } catch (Exception e) {
            throw new LyException(ExceptionEnum.INVALID_NOTIFY_SIGN);
        }
    }
}
